package com.hutianao.gqgl.job.control;


import com.hutianao.gqgl.job.entity.SearchJobEntity;
import com.hutianao.gqgl.job.service.JobService;
import com.hutianao.gqgl.menu.entity.SearchMenuEntity;
import com.hutianao.gqgl.menu.service.MenuService;
import com.hutianao.gqgl.mybatis.entity.LogEntity;
import com.hutianao.gqgl.mybatis.entity.PtJobEntity;
import com.hutianao.gqgl.mybatis.entity.PtMenuEntity;
import com.hutianao.gqgl.mybatis.entity.PtPostEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PaddingEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PadingRstType;
import com.hutianao.gqgl.mybatis.ztree.ZtreeEntity;
import com.hutianao.gqgl.spring.control.BaseController;
import com.hutianao.gqgl.spring.tag.entity.DictTagEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import org.springframework.stereotype.Service;
@Controller
@RequestMapping("/post")
public class JobController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private JobService jobService;

    @RequestMapping("/page")
    private String postPage(HttpServletRequest request){
        List<DictTagEntity> roleList=jobService.queryRoleList();
        List<DictTagEntity> organList=jobService.queryOrganList();
        request.getSession().setAttribute("roleList",roleList);
        request.getSession().setAttribute("organList",organList);
        return "job/jobPage";

    }

    @RequestMapping("/queryLeftOrganTree")
    @ResponseBody
    private List<ZtreeEntity> queryLeftOrganTree(){
        return jobService.queryLeftOrganTree();
    }

    @RequestMapping("/queryPostInfoListByOrgan")
    @ResponseBody
    public PadingRstType<PtJobEntity> queryPostInfoListByOrgan(SearchJobEntity search, PaddingEntity padding){
        logger.info(search);
        logger.info(padding);
        PadingRstType<PtJobEntity> padingRstType=jobService.queryPostInfoListByOrgan(search,padding);
        return padingRstType;
    }

    @RequestMapping("/queryPostInfoById")
    @ResponseBody
    public PtJobEntity queryPostInfoById(@RequestParam("dutyId") String dutyId){
        logger.info("dutyId:"+dutyId);
        PtJobEntity ptPostEntity=jobService.queryPostInfoById(dutyId);
        return ptPostEntity;
    }

    @RequestMapping("/jobPageDetail")
    private String postPageDetail(@RequestParam("dutyId") String dutyId,HttpServletRequest request){
        logger.info("dutyId:" + dutyId);
        request.getSession().setAttribute("dutyId",dutyId );
        return "job/jobPageDetail";
    }

    @RequestMapping("/addPostInfo")
    @ResponseBody
    public String addPostInfo(PtJobEntity ptPostEntity){
        logger.info(ptPostEntity);
        jobService.addPostInfo(ptPostEntity);
        return jsonSuccess("post.add.success");
    }

    @RequestMapping("/modifyPostInfo")
    @ResponseBody
    public String modifyPostInfo(PtJobEntity ptPostEntity){
        logger.info(ptPostEntity);
        jobService.modifyPostInfo(ptPostEntity);
        return jsonSuccess("post.modify.success");
    }

    @RequestMapping("/deletePostById")
    @ResponseBody
    public String deletePostByid(@RequestParam("dutyIdArray") String dutyIdArray){
        logger.info("dutyIdArray:"+dutyIdArray);
        try {
            jobService.deletePostById(dutyIdArray);
        } catch (UncategorizedSQLException e) {
            e.printStackTrace();
            return jsonFail("post.delete.fail");
        }
        return jsonSuccess("post.delete.success");
    }

    @RequestMapping("/queryRoleMenuList")
    @ResponseBody
    public PadingRstType<PtMenuEntity> queryRoleMenuList(SearchMenuEntity search, PaddingEntity paddingEntity){
        logger.info(search);
        logger.info(paddingEntity);
        LogEntity logEntity = new LogEntity();
        logEntity.setUseruuid("id");
        logEntity.setLogaction("查询");
        logEntity.setLogmodule("配置管理");
        logEntity.setLogsystem("子系统");
        logEntity.setLogcontents("删除配置");
        //操作日志接口
        // logService.saveLog(logEntity);
        PadingRstType<PtMenuEntity> padingRstType=jobService.queryRoleMenuList(search,paddingEntity);

        return padingRstType;
    }
    @RequestMapping("/403")
    public String show403() {
        return "/error/403";
    }
}
